//File name: PERSON_LIB.H
#ifndef PERSON_LIB_H_
#define PERSON_LIB_H_

#include "Person.h"

#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>

void Add_Person(string s, Person l[], int &numberPerson){
   string temp, a_fname, a_dob, a_addr;
   int a_id;

   int mark = s.find(":");
   a_id = atoi(s.substr(0,mark).c_str());


   temp = s.substr(mark+1);
   mark = temp.find(":");
   a_fname = temp.substr(0,mark);

   temp = temp.substr(mark+1);
   mark = temp.find(":");
   a_dob = temp.substr(0,mark);

   temp = temp.substr(mark+1);
   mark = temp.find(":");
   a_addr = temp.substr(0,mark);


   Person Per(a_id, a_fname, a_dob, a_addr);
   l[numberPerson] = Per;
   numberPerson = numberPerson + 1;
}

void ReadFile(string _file, Person l[], int &numberPerson){
               ifstream fin;
               fin.open(_file.c_str());
               if(!fin.is_open()){
                       cout<<"Cannot open file.\n";
                       exit(1);
               }else{
                              string s = "";
                              while (getline(fin, s)){
                                Add_Person(s,l,numberPerson);
                              }
                              fin.close();
               }
}

void WriteFile(string _file, Person l[], int &numberPerson){
               ofstream fout;
               fout.open(_file.c_str());
               for (int i=0 ; i<numberPerson ; i++){
                              fout<<l[i].get_id()<<":"<<l[i].get_fname()<<":"<<l[i].get_dob()<<":"<<l[i].get_addr()<<":"<<endl;
               }
               fout.close();
}
#endif
